package com.example.com.itfanr.ui.update;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView mTextIvew;
	private Button mButton;
	private final int TIME_ADD = 3;
	private int current = 0;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case TIME_ADD:
				mTextIvew.setText(Integer.toString(current, 10));
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.mTextIvew = (TextView) findViewById(R.id.mTextView);
		mTextIvew.setText(Integer.toString(current, 10));
		this.mButton = (Button) findViewById(R.id.mButton);
		this.mButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					@Override
					public void run() {
						while (true) {
							current++;
							try {
								Thread.sleep(300);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							  Message msg = Message.obtain(handler);  
		                        msg.what = TIME_ADD;  
		                        handler.sendMessage(msg);  
						}

					}
				}).start();
			}
		});
	}
}
